package com.xyzq.simpson.apm.web.service.influx;

import com.xyzq.simpson.apm.web.utils.TimeUtils;
import com.xyzq.simpson.base.type.Table;
import com.xyzq.simpson.hibbert.storage.influx.InfluxDBConnection;
import com.xyzq.simpson.hibbert.storage.influx.InfluxdbConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.influxdb.dto.QueryResult;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @date 2021/5/27 13:35
 */
public class HttpExecuteService {
    private static final String HTTP_EXECUTE = "http-execute";
    private static final String RPC_EXECUTE = "rpc-execute";

    private static Logger logger = LoggerFactory.getLogger(HttpExecuteService.class);

    public static List<Map<String, Object>> getGlobalCPMHttp(long from, long to) {
        String originSql = "select mean(count) as meanCount from \"http-execute\" where time>=$startTi and time<=$endTi " +
                "group by project fill(0) tz('Asia/Shanghai')";
        Table<String, Object> param = new Table<>();
        param.put("startTi", TimeUtils.getStringTimeFromTimestamp(from));
        param.put("endTi",TimeUtils.getStringTimeFromTimestamp(to));
        InfluxDBConnection influxDBConnection = InfluxdbConnectionManager.fetchFixedShadingConn(HTTP_EXECUTE);
        long start = System.currentTimeMillis();
        QueryResult queryResult = influxDBConnection.queryByCommandAndParam(originSql, param);
        logger.info("HttpExecuteService-getGlobalCPMHttp ...time:" + (System.currentTimeMillis()-start) + "ms.");
        List<Map<String, Object>> rawList = ResultWrapper.wrapperSeries(queryResult);
        Collections.sort(rawList, (o1, o2) -> {
            double d1 =  (double)o1.get("meanCount");
            double d2 =  (double)o2.get("meanCount");
            if(d1 > d2) {
                return -1;
            } else if(d1 < d2) {
                return 1;
            } else {
                return 0;
            }
        });
        return rawList;
    }

    public static List<Map<String, Object>> getGlobalCPMRpc(long from, long to) {
        String originSql = "select mean(count) as meanCount from \"rpc-execute\" where time>=$startTi and time<=$endTi " +
                "group by project fill(0) tz('Asia/Shanghai')";
        Table<String, Object> param = new Table<>();
        param.put("startTi", TimeUtils.getStringTimeFromTimestamp(from));
        param.put("endTi",TimeUtils.getStringTimeFromTimestamp(to));
        InfluxDBConnection influxDBConnection = InfluxdbConnectionManager.fetchFixedShadingConn(RPC_EXECUTE);
        long start = System.currentTimeMillis();
        QueryResult queryResult = influxDBConnection.queryByCommandAndParam(originSql, param);
        logger.info("HttpExecuteService-getGlobalCPMRpc ...time:" + (System.currentTimeMillis()-start) + "ms.");
        List<Map<String, Object>> rawList = ResultWrapper.wrapperSeries(queryResult);
        Collections.sort(rawList, (o1, o2) -> {
            double d1 =  (double)o1.get("meanCount");
            double d2 =  (double)o2.get("meanCount");
            if(d1 > d2) {
                return -1;
            } else if(d1 < d2) {
                return 1;
            } else {
                return 0;
            }
        });
        return rawList;
    }

}
